﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using DocumentManagement.ViewsModels;
using System.IO;
using DocumentManagement.Models;
using System.Data;
using DocumentManagement.Views;

namespace DocumentManagement
{
    /// <summary>
    /// Interaction logic for AddDocumentIn.xaml
    /// </summary>
    public partial class AddDocumentIn : Window
    {

        //private ContextMenu _contextMenu = null;
        AddDocumentInViewsModel _addDocumentInViewsModel;

        public AddDocumentIn(Receive receive, QSHCEntities1 entity)
        {
            InitializeComponent();

            _addDocumentInViewsModel = this.Resources["addDocumentInViewsModel"] as AddDocumentInViewsModel;
            _addDocumentInViewsModel.MyReceive = receive;       
            _addDocumentInViewsModel.EntityModels = entity;

            switch (receive.Type)
            {
                case 1:
                    _addDocumentInViewsModel.MyStatusDoc1 = true;
                    break;
                case 2:
                    _addDocumentInViewsModel.MyStatusDoc2 = true;
                    break;

                default:
                    break;
            }
            

            switch (receive.Speed)
            {
                case 1:
                    _addDocumentInViewsModel.MySpeed1 = true;
                    break;
                case 2:
                    _addDocumentInViewsModel.MySpeed2 = true;
                    break;
                case 3:
                    _addDocumentInViewsModel.MySpeed3 = true;
                    break;
                case 4:
                    _addDocumentInViewsModel.MySpeed4 = true;
                    break;

                default:
                    break;
            }
            switch (receive.Action)
            {
                case 1:
                    _addDocumentInViewsModel.MyAction1 = true;
                    break;
                case 2:
                    _addDocumentInViewsModel.MyAction2 = true;
                    break;

                default:
                    break;
            }
            foreach (var item in receive.ReceiveAttachedFiles)
            {
                _addDocumentInViewsModel.MyReceive.ReceiveAttachedFiles.Add(item);
            }
      


        }

    

        private void deleteINDocMenu_Click(object sender, RoutedEventArgs e)
        {
           
            _addDocumentInViewsModel.DeleteFile();
            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ToDivisionIDcomboBox.SelectedValue = 17;
            ToDivisionIDcomboBox.IsEnabled = false;
            if(_addDocumentInViewsModel.MyReceive.EntityState == EntityState.Added || _addDocumentInViewsModel.MyReceive.EntityState == EntityState.Detached)
                _addDocumentInViewsModel.GetAutoNumber();
        
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {

            if (StatusDocRadioButton1.IsChecked == false && StatusDocRadioButton2.IsChecked == false)
            {
                MessageBox.Show("กรุณาเลือกชนิดหนังสือ", "กรอกข้อมูลไม่ครบ");

                return;
            }

                if (DocumentCodetextBox.Text == "")
                {
                    MessageBox.Show("กรุณากรอกเลขที่ในหนังสือ", "กรอกข้อมูลไม่ครบ",MessageBoxButton.OK);
                    DocumentCodetextBox.Focus();
                    return;
                }
                if (FormDivisionIDcomboBox.SelectedValue == null)
                {
                    MessageBox.Show("กรุณาเลือกจากหน่วยงาน", "กรอกข้อมูลไม่ครบ", MessageBoxButton.OK);
                    return;
                }
                if (ToDivisionIDcomboBox.SelectedValue == null)
                {
                    MessageBox.Show("กรุณาเลือกถึงหน่วยงาน", "กรอกข้อมูลไม่ครบ", MessageBoxButton.OK);
                    return;
                }
                if (TitletextBox.Text == "")
                {
                    MessageBox.Show("กรุณากรอกชื่อเรื่องหนังสือ", "กรอกข้อมูลไม่ครบ", MessageBoxButton.OK);
                    TitletextBox.Focus();
                    return;
                }
                if (SpeedRadio1.IsChecked == false && SpeedRadio2.IsChecked == false && SpeedRadio3.IsChecked == false && SpeedRadio4.IsChecked == false)
                {
                    MessageBox.Show("กรุณาเลือกชั้นความเร็วของหนังสือ", "กรอกข้อมูลไม่ครบ", MessageBoxButton.OK);
                    return;
                }
                if (ActionRadioButton1.IsChecked == false && ActionRadioButton2.IsChecked == false)
                {
                    MessageBox.Show("กรุณาเลือกการปฏิบัติของหนังสือ", "กรอกข้อมูลไม่ครบ", MessageBoxButton.OK);
                    return;
                }
                  _addDocumentInViewsModel.SaveToDatabase();
                  PageSwitcher.Switch(new ShowItemDocumentInUserControl());
                  //ShowItemDocumentInUserControl mm = new ShowItemDocumentInUserControl();
                  //mm.Visibility = Visibility.Collapsed;
                  this.Close();
             
          
            }

        private void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            _addDocumentInViewsModel.GetBrowse();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void StatusDocRadioButton1_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void StatusDocRadioButton2_Checked(object sender, RoutedEventArgs e)
        {
            //if (_addDocumentInViewsModel.MyReceive.EntityState == EntityState.Added || _addDocumentInViewsModel.MyReceive.EntityState == EntityState.Detached)
            //    _addDocumentInViewsModel.GetAutoNumber();
        }

        }

   

       
     
       

     
     
     

       
       

      
    }

